package com.bandaotixi.cashier.api.system.service;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.auto.SaTokenDaoByObjectFollowString;
import com.bandaotixi.cashier.api.system.mapper.SysKeyValueMapper;
import com.bandaotixi.cashier.api.system.model.SysKeyValue;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysKeyValueService extends MPJBaseServiceImpl<SysKeyValueMapper, SysKeyValue> implements SaTokenDaoByObjectFollowString, SaTokenDao {
    private SysKeyValue getOrRemove(String key) {
        SysKeyValue sysKeyValue = lambdaQuery().eq(SysKeyValue::getStoreKey, key).one();
        if (sysKeyValue != null) {
            if (sysKeyValue.getStoreExpire() != null && sysKeyValue.getStoreExpire().isBefore(LocalDateTime.now())) {
                removeById(sysKeyValue.getId());
                return null;
            }
            return sysKeyValue;
        }
        return null;
    }

    @Override
    public String get(String key) {
        SysKeyValue sysKeyValue = getOrRemove(key);
        return sysKeyValue != null ? sysKeyValue.getStoreValue() : null;
    }

    @Override
    public void set(String key, String value, long timeout) {
        if (timeout != 0L && timeout > -2L) {
            SysKeyValue sysKeyValue = lambdaQuery().eq(SysKeyValue::getStoreKey, key).one();
            if (sysKeyValue == null) {
                sysKeyValue = new SysKeyValue();
                sysKeyValue.setStoreKey(key);
            }
            sysKeyValue.setStoreValue(value);
            sysKeyValue.setStoreExpire(timeout > 0 ? LocalDateTime.now().plusSeconds(timeout) : null);
            saveOrUpdate(sysKeyValue);
        }
    }

    @Override
    public void update(String key, String value) {
        long expire = this.getTimeout(key);
        if (expire != -2L) {
            this.set(key, value, expire);
        }
    }

    @Override
    public void delete(String key) {
        lambdaUpdate().eq(SysKeyValue::getStoreKey, key).remove();
    }

    @Override
    public long getTimeout(String key) {
        SysKeyValue sysKeyValue = lambdaQuery().eq(SysKeyValue::getStoreKey, key).one();
        if (sysKeyValue != null) {
            if (sysKeyValue.getStoreExpire() != null && sysKeyValue.getStoreExpire().isBefore(LocalDateTime.now())) {
                removeById(sysKeyValue.getId());
                return -2L;
            }
            if (sysKeyValue.getStoreExpire() == null) {
                return -1;
            }
            return Duration.between(LocalDateTime.now(), sysKeyValue.getStoreExpire()).getSeconds();
        }
        return -2;
    }

    @Override
    public void updateTimeout(String key, long timeout) {
        SysKeyValue one = lambdaQuery().eq(SysKeyValue::getStoreKey, key).one();
        if (timeout == -1L) {
            if (one.getStoreExpire() != null) {
                one.setStoreExpire(null);
                updateById(one);
            }

        } else if (timeout <= 0) {
            removeById(one.getId());
        } else {
            one.setStoreExpire(LocalDateTime.now().plusSeconds(timeout));
            updateById(one);
        }
    }

    @Override
    public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
        String sizeSql = size == -1 ? "" : "," + size;
        return lambdaQuery()
                .like(SysKeyValue::getStoreKey, prefix + "%" + keyword + "%")
                .orderBy(true, sortType, SysKeyValue::getStoreKey)
                .last("limit " + start + sizeSql)
                .list().stream().map(SysKeyValue::getStoreKey)
                .toList();
    }
}
